package com.clear.honghefinance.biz.modules.system.member.job;

import com.clear.honghefinance.wechat.config.wechat.WechatConfig;
import com.clear.honghefinance.wechat.service.WechatService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class WechatJobService {

    @Resource
    private WechatService wechatService;
    @Resource
    private WechatConfig wechatConfig;

    /**
     * 设置微信公众号token缓存（Bean模式）
     */
    @XxlJob("setMpAccessTokenCacheJobHandler")
    public ReturnT<String> setMpAccessTokenCacheJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, setMpAccessTokenCacheJobHandler -> begin");

        try {
            XxlJobHelper.log("更新微信公众号token缓存...");
            wechatService.setMpAccessTokenCache(wechatConfig.getMp().getAppId(), wechatConfig.getMp().getSecret());
            XxlJobHelper.log("微信公众号token缓存更新成功...");
        } catch (Exception e) {
            XxlJobHelper.log("微信公众号token缓存更新失败...");
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

}
